/*
    api:"添加职位类型"
    name:"陈小洋"
    time：2021.1.24
* */
const PostType = require('../../../control/PostType/PostType')
const tools = require('../../../tools/public');
/**
 * @api {post} /postType/addPostType 1.添加职位类型
 * @apiGroup PostType
 *
 * @apiParam {Number} level 职位层级
 * @apiParam {String} PostTypeName 职位名称
 * @apiParam {String} [PostTypeDescribe] 职位描述
 * @apiParam {String} [parentId] 父级id
 *
 *
 *
 * @apiParamExample {json} Request-Example
 *{
 *
 *}
 * @apiSuccessExample  {json} Response-Example
 * {
 *   "code": 200,
 *   "msg": "添加成功！"
 *   }
 */
module.exports = async function (req, res, next) {
    let { level,parentId,PostTypeName, PostTypeDescribe} = req.body,
        result = null;
    try {
        if (!level || !PostTypeName) return res.send(tools.ReturnData(204, '参数错误,请认真阅读接口文档！'));

        if(level==1){ // 一级职位
            let count = await PostType.Sel_Count({'PostTypeName':PostTypeName})
            if(count>0) return res.send(tools.ReturnData(204, '该职位已存在！'));
            result = await PostType.Add_level1({PostTypeName,PostTypeDescribe});
            res.send(tools.ReturnData(200, '一级职位添加成功！',result));
        }else{ // 二级职位
            result = await PostType.Add_level2(parentId,{PostTypeName,PostTypeDescribe});
            res.send(tools.ReturnData(result.code, result.msg,result.data));
        }
    } catch (e) {
        console.log(e)
        return res.send(tools.ReturnData(500, '系统内部错误！'));
    }
};